package com.shengqi.proxy_hook.hook.activityhook;

import android.text.TextUtils;
import android.util.Log;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class LanguageManager {
    private static final String TAG = "LanguageManager";
    public static HashMap<String, String[]> stringArrayHashMap;
    public static HashMap<String, String> stringHashMap;

    public static void doTest() {
        try {
            long startTime = System.currentTimeMillis();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            //解析XML输入流，得到Document对象，表示一个XML文档
            Document document = builder.parse(new File("/sdcard/values-zh-rCN.xml"));
            //获得文档中的次以及节点，persons

            Element element = document.getDocumentElement();
            // 获取Element下一级的person节点集合，以NodeList的形式存放。
            Log.e(TAG, "element = " + element.getTagName());

            NodeList stringArrayNodes = element.getElementsByTagName("string-array");
            Log.e(TAG, "stringArrayNodes.size = " + stringArrayNodes.getLength());
            stringArrayHashMap = new HashMap();
            for (int i = 0; i < stringArrayNodes.getLength(); i++) {
                ArrayList<String> stringList = new ArrayList<>();
                NodeList childNodes = stringArrayNodes.item(i).getChildNodes();
                for (int j = 0; j < childNodes.getLength(); j++) {
                    if (childNodes.item(j).getNodeType() == 1) {
                        stringList.add(childNodes.item(j).getTextContent());
                        Log.e(TAG, stringArrayNodes.item(i).getAttributes().getNamedItem("name").getNodeValue() + " = " + childNodes.item(j).getTextContent());
                    }
                }
                stringArrayHashMap.put(stringArrayNodes.item(i).getAttributes().getNamedItem("name").getNodeValue(), stringList.toArray(new String[stringList.size()]));
            }

            NodeList stringNodes = element.getElementsByTagName("string");
            Log.e(TAG, "stringNodes.size = " + stringNodes.getLength());

            stringHashMap = new HashMap();
            for (int i = 0; i < stringNodes.getLength(); i++) {
                stringHashMap.put(stringNodes.item(i).getAttributes().getNamedItem("name").getNodeValue(), stringNodes.item(i).getTextContent());
            }
            Log.e(TAG, "time cost = " + (System.currentTimeMillis() - startTime) + " ms");
            Log.e(TAG, "look up trading_transactions :" + stringHashMap.get("trading_transactions"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
